﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using Terraria.GameContent.Liquid;

namespace Terraria.ModLoader
{
	// Token: 0x0200020B RID: 523
	public static class WaterStyleLoader
	{
		// Token: 0x170001B5 RID: 437
		// (get) Token: 0x060016F8 RID: 5880 RVA: 0x00011841 File Offset: 0x0000FA41
		public static int WaterStyleCount
		{
			get
			{
				return WaterStyleLoader.nextWaterStyle;
			}
		}

		// Token: 0x060016F9 RID: 5881 RVA: 0x00417184 File Offset: 0x00415384
		internal static int ReserveStyle()
		{
			int result = WaterStyleLoader.nextWaterStyle;
			WaterStyleLoader.nextWaterStyle++;
			return result;
		}

		// Token: 0x060016FA RID: 5882 RVA: 0x00011848 File Offset: 0x0000FA48
		public static ModWaterStyle GetWaterStyle(int style)
		{
			if (style < 12 || style >= WaterStyleLoader.nextWaterStyle)
			{
				return null;
			}
			return WaterStyleLoader.waterStyles[style - 12];
		}

		// Token: 0x060016FB RID: 5883 RVA: 0x00011867 File Offset: 0x0000FA67
		internal static void ResizeArrays()
		{
			Array.Resize<Texture2D>(ref LiquidRenderer.Instance._liquidTextures, WaterStyleLoader.nextWaterStyle);
			Array.Resize<float>(ref Main.liquidAlpha, WaterStyleLoader.nextWaterStyle);
			Array.Resize<Texture2D>(ref Main.liquidTexture, WaterStyleLoader.nextWaterStyle);
		}

		// Token: 0x060016FC RID: 5884 RVA: 0x0001189B File Offset: 0x0000FA9B
		internal static void Unload()
		{
			WaterStyleLoader.nextWaterStyle = 12;
			WaterStyleLoader.waterStyles.Clear();
		}

		// Token: 0x060016FD RID: 5885 RVA: 0x004171A4 File Offset: 0x004153A4
		public static void ChooseWaterStyle(ref int style)
		{
			foreach (ModWaterStyle modWaterStyle in WaterStyleLoader.waterStyles)
			{
				if (modWaterStyle.ChooseWaterStyle())
				{
					style = modWaterStyle.Type;
				}
			}
			WorldHooks.ChooseWaterStyle(ref style);
		}

		// Token: 0x060016FE RID: 5886 RVA: 0x00417200 File Offset: 0x00415400
		public static void UpdateLiquidAlphas()
		{
			if (Main.waterStyle >= 12)
			{
				for (int i = 0; i < 12; i++)
				{
					if (i != 1 && i != 11)
					{
						Main.liquidAlpha[i] -= 0.2f;
						if (Main.liquidAlpha[i] < 0f)
						{
							Main.liquidAlpha[i] = 0f;
						}
					}
				}
			}
			foreach (ModWaterStyle modWaterStyle in WaterStyleLoader.waterStyles)
			{
				int type = modWaterStyle.Type;
				if (Main.waterStyle == type)
				{
					Main.liquidAlpha[type] += 0.2f;
					if (Main.liquidAlpha[type] > 1f)
					{
						Main.liquidAlpha[type] = 1f;
					}
				}
				else
				{
					Main.liquidAlpha[type] -= 0.2f;
					if (Main.liquidAlpha[type] < 0f)
					{
						Main.liquidAlpha[type] = 0f;
					}
				}
			}
		}

		// Token: 0x060016FF RID: 5887 RVA: 0x00417300 File Offset: 0x00415500
		public static void DrawWatersToScreen(bool bg)
		{
			for (int i = 12; i < WaterStyleLoader.nextWaterStyle; i++)
			{
				if (Main.liquidAlpha[i] > 0f)
				{
					if (bg)
					{
						if (Main.waterStyle < i)
						{
							Main.instance.DrawWater(bg, i, Main.liquidAlpha[i]);
						}
						else
						{
							Main.instance.DrawWater(bg, i, 1f);
						}
					}
					else
					{
						Main.instance.DrawWater(bg, i, Main.liquidAlpha[i]);
					}
				}
			}
		}

		// Token: 0x06001700 RID: 5888 RVA: 0x00417374 File Offset: 0x00415574
		public static void DrawWaterfall(WaterfallManager waterfallManager, SpriteBatch spriteBatch)
		{
			foreach (ModWaterStyle modWaterStyle in WaterStyleLoader.waterStyles)
			{
				if (Main.liquidAlpha[modWaterStyle.Type] > 0f)
				{
					waterfallManager.DrawWaterfall(spriteBatch, modWaterStyle.ChooseWaterfallStyle(), Main.liquidAlpha[modWaterStyle.Type]);
				}
			}
		}

		// Token: 0x06001701 RID: 5889 RVA: 0x004173E8 File Offset: 0x004155E8
		public static void LightColorMultiplier(int style, ref float r, ref float g, ref float b)
		{
			ModWaterStyle waterStyle = WaterStyleLoader.GetWaterStyle(style);
			if (waterStyle != null)
			{
				waterStyle.LightColorMultiplier(ref r, ref g, ref b);
				r *= Lighting.negLight * Lighting.blueWave;
				g *= Lighting.negLight * Lighting.blueWave;
				b *= Lighting.negLight * Lighting.blueWave;
			}
		}

		// Token: 0x040015D6 RID: 5590
		public const int vanillaWaterCount = 12;

		// Token: 0x040015D7 RID: 5591
		private static int nextWaterStyle = 12;

		// Token: 0x040015D8 RID: 5592
		internal static readonly IList<ModWaterStyle> waterStyles = new List<ModWaterStyle>();
	}
}
